package slt.Controller;

import java.io.IOException;
import java.util.List;
import slt.Dao.MessageDAO;
import slt.Model.Message;
import slt.Model.User;

public class MessageController extends ControllerServlet {
    private final MessageDAO messageDAO = new MessageDAO();

    public void add() {
        if(request.getParameter("submit") != null) { 
            
            Message m = new Message();
            m.setMessage(request.getParameter("message"));
            //User tempuser = (User)session.getAttribute("sessionUser");
            //m.setMessagedBy(tempuser);
            messageDAO.insert(m);
            redirect("../message/view"); // nu sturen we hem dus naar de controller message/view (oww das hieronder xD)
        }
    
        
          }
    public void view() {
         //if(request.getParameter("id") != null) {
            //long id = Long.parseLong(request.getParameter("id")); er is geen id
            //Message message = messageDAO.getById(id);
        
        String parameter = request.getParameter("username");
            List<Message> messages = messageDAO.listAll();
            //waar klapt ie xD
            set("messages", messages);
            //set("date", message.getDate());
            
            render("view_topic"); //joram zijn render/redirect... render heeft geen .jsp nodig xD
         //} 
    }
    
    public void json() throws IOException {
       // response.getWriter().println("{\"messages\":" + messageDAO.listAllJson() + "}");
    }    
}